﻿using System;
using System.Linq;
using DNT.RavenQA.Core.Extensions;
using DNT.RavenQA.Core.Models;
using Raven.Client;

namespace DNT.RavenQA.Web.Infrastructure.QueryModel
{
    public class HottestQuestionsQuery : IQuery<Question>
    {
        public HottestQuestionsQuery(Func<DateTime> date)
        {
            date.EnsureIsNotNull("date");
            _date = date;
        }

        private readonly Func<DateTime> _date;

        public IQueryable<Question> ExecuteWithStats(IDocumentSession session,
                                                     out RavenQueryStatistics stats)
        {
            return QuestionQueryHelper.GetBaseQuery(session)
                .Statistics(out stats)
                .Where(q => q.LastUpdateOn > _date())
                .OrderByDescending(question => question.AnswersCount);
        }

        public IQueryable<Question> Execute(IDocumentSession session)
        {
            return QuestionQueryHelper.GetBaseQuery(session)
                .Where(q => q.LastUpdateOn > _date())
                .OrderByDescending(question => question.AnswersCount);
        }
    }
}